/*
** Copyright (C) 2010 Calin Radoni
**
** This file is part of CHScanner.
**
** CHScanner is free software: you can redistribute it and/or modify
** it under the terms of the GNU General Public License as published by
** the Free Software Foundation, either version 3 of the License, or
** (at your option) any later version.
**
** CHScanner is distributed in the hope that it will be useful,
** but WITHOUT ANY WARRANTY; without even the implied warranty of
** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
** GNU General Public License for more details.
**
** You should have received a copy of the GNU General Public License
** along with CHScanner. If not, see <http://www.gnu.org/licenses/>.
*/

// DlgInterface.cpp : implementation file

#include "stdafx.h"
#include "CHScanner.h"
#include "DlgInterface.h"

//---------------------------------------------------------------------------

IMPLEMENT_DYNAMIC(DlgInterface, CRgnDialog)
DlgInterface::DlgInterface(CWnd* pParent /*=NULL*/) : CRgnDialog(DlgInterface::IDD, pParent)
{
	skinIn = NULL;
	winPcap = NULL;
}

DlgInterface::~DlgInterface()
{
}

//---------------------------------------------------------------------------

void DlgInterface::DoDataExchange(CDataExchange* pDX)
{
	CRgnDialog::DoDataExchange(pDX);
	DDX_Control(pDX, IDC_LB_INTF, lbIntf);
	DDX_Control(pDX, IDC_LBL_INFO, lblInfo);
	DDX_Control(pDX, IDC_BTN_OK, btnOK);
}

//---------------------------------------------------------------------------

BEGIN_MESSAGE_MAP(DlgInterface, CRgnDialog)
	ON_BN_CLICKED(IDC_BTN_OK, &DlgInterface::OnBnClickedBtnOk)
	ON_LBN_DBLCLK(IDC_LB_INTF, &DlgInterface::OnLbnDblclkLbIntf)
	ON_LBN_SELCHANGE(IDC_LB_INTF, &DlgInterface::OnLbnSelchangeLbIntf)
END_MESSAGE_MAP()

//---------------------------------------------------------------------------

BOOL DlgInterface::OnInitDialog()
{
	int i, cnt;

	CRgnDialog::OnInitDialog();

	intfName.Empty();

	cnt = 0;

	Clear();
	if(winPcap!=NULL){
		cnt = winPcap->GetIpInterfaceCount();
		for(i=0; i<cnt; i++)
			AddData(winPcap->GetIpInterfaceInfo(i));
	}

	if(cnt>1){
		lbIntf.SetCurSel(0);
		OnLbnSelchangeLbIntf();
	}
	else{
		lblInfo.SetWindowText(_T(""));
	}

	// TODO:  Add extra initialization here

	SetProperties(skinIn, false, _T("Interface"));

	if(skin!=NULL)
		lblInfo.textColor = skin->textColor;

	return TRUE;  // return TRUE unless you set the focus to a control
}

//---------------------------------------------------------------------------

void DlgInterface::OnOK()
{
	// CRgnDialog::OnOK();
}

void DlgInterface::OnCancel()
{
	// CRgnDialog::OnCancel();
}

//---------------------------------------------------------------------------

void DlgInterface::OnBnClickedBtnOk()
{
	MIpInterfaceInfo *ii;
	int idx;

	idx = lbIntf.GetCurSel();
	if(idx==LB_ERR) return;

	ii = (MIpInterfaceInfo*)lbIntf.GetItemDataPtr(idx);
	if(ii==NULL) return;

	intfName = ii->intfName;

	EndDialog(IDOK);
}

//---------------------------------------------------------------------------

void DlgInterface::OnLbnDblclkLbIntf()
{
	MIpInterfaceInfo *ii;
	int idx;

	idx = lbIntf.GetCurSel();
	if(idx==LB_ERR) return;

	ii = (MIpInterfaceInfo*)lbIntf.GetItemDataPtr(idx);
	if(ii==NULL) return;

	intfName = ii->intfName;

	EndDialog(IDOK);
}

//---------------------------------------------------------------------------

void DlgInterface::OnLbnSelchangeLbIntf()
{
	MIpInterfaceInfo *ii;
	CString str, sss;
	int idx;

	str.Empty();

	idx = lbIntf.GetCurSel();
	if(idx==LB_ERR){
		lblInfo.SetWindowText(str);
		Invalidate(TRUE);
		UpdateWindow();
		return;
	}

	ii = (MIpInterfaceInfo*)lbIntf.GetItemDataPtr(idx);
	if(ii==NULL){
		lblInfo.SetWindowText(str);
		Invalidate(TRUE);
		UpdateWindow();
		return;
	}

	sss.Format(_T("Interface name: %s\n"), ii->intfName);
	str += sss;

	if(!ii->IsIPv4Zero()){
		sss.Format(_T("IPv4 Address: %s\n"), ii->GetIPv4Name());
		str += sss;
	}

	if(!ii->IsIPv6Zero()){
		sss.Format(_T("IPv6 Address: %s\n"), ii->GetIPv6Name());
		str += sss;
	}

	lblInfo.SetWindowText(str);
	Invalidate(TRUE);
	UpdateWindow();
}

//---------------------------------------------------------------------------

void DlgInterface::ResizeChilds(void)
{
	int xx, yy, ww, hh;
	int bw, bh;
	int ew, eh;
	int lw, lh;
	int dd;
	RECT rect;

	dd = 2;

	xx = clientRect.left + dd;
	yy = clientRect.top + dd;
	ww = clientRect.right - clientRect.left;
	hh = clientRect.bottom - clientRect.top;

	btnOK.GetWindowRect(&rect);
	bw = rect.right - rect.left;
	bh = rect.bottom - rect.top;

	lblInfo.GetWindowRect(&rect);
	ew = ww - dd - dd;
	eh = rect.bottom - rect.top;

	lw = ew;
	lh = hh - dd - bh - dd - eh - dd;

	lbIntf.MoveWindow(xx, yy, lw, lh, TRUE);

	yy += lh;
	yy += dd;
	lblInfo.MoveWindow(xx, yy, ew, eh, TRUE);

	yy += eh;
	yy += dd;
	xx = ww - bw;
	btnOK.MoveWindow(xx, yy, bw, bh);
}

bool DlgInterface::GetMinimumSize(RECT* rectIn)
{
	if(rectIn==NULL) return false;

	rectIn->left   = 0;
	rectIn->right  = 520;
	rectIn->top    = 0;
	rectIn->bottom = 130;

	return true;
}

//---------------------------------------------------------------------------

void DlgInterface::Clear(void)
{
	while(lbIntf.GetCount()>0)
		lbIntf.DeleteString(0);
}

bool DlgInterface::AddData(MIpInterfaceInfo *iiIn)
{
	int idx, res;
	CString str, sss;

	if(iiIn==NULL) return true;

	str = iiIn->intfDesc;
	sss = str.Left(16);
	if(sss.CompareNoCase(_T("Network adapter "))==0)
		str = str.Mid(16);

	idx = lbIntf.AddString(str);
	if(idx<0) return false;

	res = lbIntf.SetItemDataPtr(idx, (void*)iiIn);
	if(res==LB_ERR){
		lbIntf.DeleteString(idx);
		return false;
	}

	return true;
}

//---------------------------------------------------------------------------
